package top.kaoshanji.p1ch05a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import top.kaoshanji.p1ch04a.Dish;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/2/11 10:42
 */
public class Mapping {

    private static final Logger logger = LogManager.getLogger(Mapping.class);

    public static void main(String[] args) {

        // 代码清单-p1ch05a-91页a：对流中每一个元素应用函数
        logger.info("...对流中每一个元素应用函数...");
        List<String> dishNames = Dish.menu.stream()
                .map(Dish::getName)
                .collect(Collectors.toList());
        System.out.println(dishNames);

        List<String> words = Arrays.asList("Hello", "World");
        List<Integer> wordLengths = words.stream()
                .map(String::length)
                .collect(Collectors.toList());
        System.out.println(wordLengths);


        // 代码清单-p1ch05a-93页a：使用flatMap
        logger.info("...使用flatMap...");
        words.stream()
                .flatMap((String line) -> Arrays.stream(line.split("")))
                .distinct()
                .forEach(System.out::println);

    }

}
